//
//  TTCommitSuccessViewController.m
//  TaoTuo
//
//  Created by admin on 2019/10/10.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTCommitSuccessViewController.h"
#import "TTBankCardModel.h"
#import "TTBankCardViewCell.h"
#import "TTMyOrderViewController.h"
#import <UIImageView+WebCache.h>
#import "TTConfirmSuccessHintView.h"
#import <UIView+TYAlertView.h>

static NSString *cellID = @"cell_id";
static NSString *emptyCellID = @"empty_cell_id";

@interface TTCommitSuccessViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSArray *titlesArray;

@end

@implementation TTCommitSuccessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fd_interactivePopDisabled = YES;
    self.title = @"提交订单";
    self.titlesArray = @[@"请您将货款汇至以下任意账户", @"您的专属业务顾问"];
    if (!self.isFromDianpu) {
        // 订单提交成功，通知购物车刷新
        [[TTLoginSingle sharedSingle].shopCartSubject sendNext:@(1)];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[TTLoginSingle sharedSingle].shopCartSelectBtnSubject sendNext:@(1)];
        });
        // 购物车数量
        [TTCommonNetData getShopCartCodeInfoSuccess:^(NSArray * _Nonnull cartCodeArray, int shopCount) {} failure:^{}];
    }
    
    // 我的页面数字更新
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshPageCountNotify" object:nil];
    
    [self getBankListData];
    
    
    TTConfirmSuccessHintView *hintView = [TTConfirmSuccessHintView createViewFromNib];
    [hintView showInWindowWithBackgoundTapDismissEnable:YES];
    
    
}

- (void)backAction
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = self.dataArray[indexPath.section];
    if (indexPath.section == 0) {
        TTBankCardViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        cell.model = arr[indexPath.row];
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:emptyCellID];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 10)];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *title = self.titlesArray[section];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 50)];
    view.backgroundColor = [UIColor whiteColor];
    [LLLineUtilies addLineInView:view startPoint:CGPointZero endPoint:CGPointMake(view.width, 0) lineColor:kSeparatorColor lineWidth:10];
    UILabel *label = [UILabel labelWithTitle:title color:UIColorHex(282828) font:[UIFont fontWithName:@"Source Han Sans CN" size: 14]];
    label.frame = CGRectMake(10, 10, view.width - 20, view.height - 10);
    [view addSubview:label];
    UIColor *bottomLineColor = section == 0 ? [UIColor whiteColor] : kSeparatorColor;
    [LLLineUtilies addLineInView:view startPoint:CGPointMake(10, view.bottom) endPoint:CGPointMake(view.width - 10, view.bottom) lineColor:bottomLineColor];
    return view;
}

// 银行卡列表
- (void)getBankListData
{
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:bankCardList_api parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableArray *arr = @[].mutableCopy;
        for (NSDictionary *dic in responseObject) {
            TTBankCardModel *bankModel = [TTBankCardModel modelWithJSON:dic];
            [arr addObject:bankModel];
        }
        
        [self.dataArray addObject:arr];
        [self.dataArray addObject:@[]];
        
        [self setupHeaderView];
        [self setupFooterView];
        [self addBottomView];
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.view];
}

// 联系客服
- (void)contactKeFuBtnClick
{
    [[TTLoginSingle sharedSingle].loginModel.SalesmanMobile callPhone];
}

// 查看订单
- (void)orderBtnClick
{
    TTMyOrderViewController *orderVC = [[TTMyOrderViewController alloc] init];
    [self.navigationController pushViewController:orderVC animated:YES];
}


- (void)setupHeaderView
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 212 + 88 * kScale)];
    headerView.backgroundColor = kSeparatorColor;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.image = [UIImage imageNamed:@"paysuccess"];
    
    UILabel *successLabel = [UILabel labelWithTitle:@"提交成功" color:[UIColor blackColor] font:[UIFont fontWithName:@"Source Han Sans CN" size:18]];
    
    UILabel *hintLabel = [UILabel labelWithTitle:@"请等待营业员联系您进行确认" color:UIColorHex(666666) font:[UIFont systemFontOfSize:12]];
    
    UIStackView *stView = [[UIStackView alloc] initWithFrame:CGRectMake(0, 50, headerView.width, 152)];
    stView.axis = UILayoutConstraintAxisVertical;
    stView.alignment = UIStackViewAlignmentCenter;
    stView.distribution = UIStackViewDistributionFill;
    stView.spacing = 10;
    
    [stView addArrangedSubview:imageView];
    [stView addArrangedSubview:successLabel];
    [stView addArrangedSubview:hintLabel];
    
    [headerView addSubview:stView];
    
    
    UIImageView *processImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, stView.bottom + 10, headerView.width, 88 * kScale)];
    processImageView.contentMode = UIViewContentModeScaleAspectFill;
    processImageView.image = [UIImage imageNamed:@"orderprocess"];
    [headerView addSubview:processImageView];
    
    self.tableView.tableHeaderView = headerView;
    
}

// footer
- (void)setupFooterView
{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 150)];
    
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 45, 45)];
    headImageView.contentMode = UIViewContentModeScaleAspectFit;
//    headImageView.image = [UIImage imageNamed:@"header_img"];
    headImageView.layer.cornerRadius = 22.5;
    headImageView.layer.masksToBounds = YES;
    [footerView addSubview:headImageView];
    NSString *headerUrlString = [[TTLoginSingle sharedSingle].loginModel.SalesmanPicture stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [headImageView sd_setImageWithURL:[NSURL URLWithString:headerUrlString] placeholderImage:[UIImage imageNamed:@"header_img"] options:SDWebImageRetryFailed];
    
    
    UILabel *nameLabel = [UILabel labelWithTitle:[TTLoginSingle sharedSingle].loginModel.SalesmanAccountName color:UIColorHex(666666) fontSize:14];
    UILabel *phoneLabel = [UILabel labelWithTitle:[TTLoginSingle sharedSingle].loginModel.SalesmanMobile color:UIColorHex(333333) fontSize:15];
    UIStackView *stView = [[UIStackView alloc] initWithFrame:CGRectMake(headImageView.right + 6, headImageView.top, footerView.width - headImageView.right - 16, headImageView.height)];
    stView.axis = UILayoutConstraintAxisVertical;
    stView.distribution = UIStackViewDistributionFillEqually;
    stView.alignment = UIStackViewAlignmentFill;
    [stView addArrangedSubview:nameLabel];
    [stView addArrangedSubview:phoneLabel];
    [footerView addSubview:stView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [[TTLoginSingle sharedSingle].loginModel.SalesmanMobile callPhone];
    }];
    [stView addGestureRecognizer:tap];
    
    self.tableView.tableFooterView = footerView;
    
}

- (void)addBottomView
{
    UIButton *kefuBtn = [UIButton createButtonWithTitle:@"联系客服" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:20] backgroundColor:UIColorHex(404469) target:self action:@selector(contactKeFuBtnClick)];
    [self.view addSubview:kefuBtn];
    [kefuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
        make.left.equalTo(self.view);
        make.width.mas_equalTo(self.view.width * 0.5);
        make.height.mas_equalTo(45);
    }];
    
    UIButton *orderBtn = [UIButton createButtonWithTitle:@"查看订单" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:20] backgroundColor:kMainColor target:self action:@selector(orderBtnClick)];
    [self.view addSubview:orderBtn];
    [orderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.width.height.equalTo(kefuBtn);
        make.left.equalTo(kefuBtn.mas_right);
    }];
    
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 130;
        _tableView.emptyImageName = @"nodata";
        _tableView.emptyTitel = @"暂无数据";
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TTBankCardViewCell class]) bundle:nil] forCellReuseIdentifier:cellID];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:emptyCellID];
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.mas_topLayoutGuideTop);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom).mas_offset(-45);
        }];
    }
    return _tableView;
}


- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


@end
